package com.suxin.module.http.callback;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.concurrent.FutureCallback;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Classname MyGracefulCloseFutureCallBack
 * @Description [ 统一计数 ]
 * @Author Tang
 * @Date 2020/10/17 10:33
 * @Created by ASUS
 */
@Slf4j
public class MyGracefulCloseFutureCallBack implements FutureCallback<HttpResponse> {

    private AtomicInteger unCompleteTask;

    private FutureCallback<HttpResponse> futureCallback;

    public MyGracefulCloseFutureCallBack(AtomicInteger unCompleteTask,FutureCallback<HttpResponse> futureCallback) {
        this.unCompleteTask = unCompleteTask;
        this.futureCallback = futureCallback;
    }

    @Override
    public void completed(HttpResponse response) {
        // 任务处理完成，在减数...
        this.futureCallback.completed(response);
        log.info("还剩余任务数是：{}",this.unCompleteTask.decrementAndGet());
    }

    @Override
    public void failed(Exception ex) {
        // 任务处理完成，在减数...
        this.futureCallback.failed(ex);
        log.info("还剩余任务数是：{}",this.unCompleteTask.decrementAndGet());
    }

    @Override
    public void cancelled() {

    }
}